/**
 * 2017-10-19 by ShiJun Liu
 *  获取id ,获取年龄
 * 
 *  
 */
module.exports = {
    getid: function() {
        var date = new Date();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var strHours = date.getHours();
        var strMinutes = date.getMinutes();
        var strSeconds = date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (strHours >= 0 && strHours <= 9) {
            strHours = "0" + strHours;
        }
        if (strMinutes >= 0 && strMinutes <= 9) {
            strMinutes = "0" + strMinutes;
        }
        if (strSeconds >= 0 && strSeconds <= 9) {
            strSeconds = "0" + strSeconds;
        }
        var currentdate = date.getFullYear() + '' + month + '' + strDate + '' + strHours + strMinutes + strSeconds + '' + (Math.floor(Math.random() * 8999) + 1000) + '';
        return currentdate;

    },
    getToday() {
        var date = new Date();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var strHours = date.getHours();
        var strMinutes = date.getMinutes();
        var strSeconds = date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (strHours >= 0 && strHours <= 9) {
            strHours = "0" + strHours;
        }
        if (strMinutes >= 0 && strMinutes <= 9) {
            strMinutes = "0" + strMinutes;
        }
        if (strSeconds >= 0 && strSeconds <= 9) {
            strSeconds = "0" + strSeconds;
        }
        var currentdate = date.getFullYear() + '/' + month + '/' + strDate + ' ' + strHours + ":" + strMinutes + ":" + strSeconds;
        return currentdate;
    },
    getNowFormatDate: function() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
            " " + date.getHours() + seperator2 + date.getMinutes() +
            seperator2 + date.getSeconds();
        return currentdate;
    },
    getDateString: function(data) {
        var date = new Date(data);
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var year = date.getFullYear();
        return year + '/' + month + '/' + strDate;
    },
    //获取年龄
    getOld: function(data, Td) {
        var date = new Date(data);
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();
        var Tdate = new Date(Td);
        var Tyear = Tdate.getFullYear();
        var Tmonth = Tdate.getMonth();
        var Tday = Tdate.getDate();
        var old = Tyear - year;
        if (Tmonth < month) {
            old--;
        }
        if (Tmonth == month) {
            if (Tday < day) {
                old--;
            }
        }
        console.log(old)
        return old;
    },
    //获取 月 天
    getMonthDay: function(data, endDate) {
        var date = new Date(data);
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();
        var Tdate = new Date(endDate);
        var Tyear = Tdate.getFullYear();
        var Tmonth = Tdate.getMonth();
        var Tday = Tdate.getDate();
        console.log(Tyear);
        if (Tyear < year) {
            return 0
        }
        //满一年
        if (Tyear > year && Tmonth >= month && Tday <= day + 1) {
            return 31 + 12;
        }
        //跨年 不足一年
        if (Tyear > year && Tmonth <= month && Tday < day) {
            return 31 + (11 - month) + Tmonth + 1;
        }
        //跨年 不足一年 多一个月
        if (Tyear > year && Tmonth < month && Tday >= day) {
            return 31 + (11 - month) + Tmonth + 1 + 1;
        }
        // 同年 多个月
        if (Tmonth > month && Tday >= day) {
            return (Tmonth - month + 1) + 31;
        }
        if (Tmonth > month && Tday == day - 1) {
            return 1 + 31;
        }
        //跨月 不足一个月
        if (Tmonth > month && Tday < day) {
            var endDay = new Date(year, month + 1, 0).getDate();
            console.log(endDay);
            return endDay - day + 1 + Tday;
        }
        if (Tmonth == month && Tday >= day) {
            var daySpan = Tday - day + 1;
            //某个月的 第一天和最后一天
            if ((daySpan == 31 || daySpan == 28 || daySpan == 30 || daySpan == 27) && day == 1) {
                var endDay = new Date(year, month + 1, 0).getDate();
                if (Tday == endDay) {
                    return 1 + 31;
                }
            }
            return daySpan;
        }
        return 0;
    },
    test: function(data, endDate) {
        var date = new Date(data);
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();
        var Tdate = new Date(endDate);
        var Tyear = Tdate.getFullYear();
        var Tmonth = Tdate.getMonth();
        var Tday = Tdate.getDate();

        var m1 = month + year * 12;
        var m2 = Tmonth + Tyear * 12;
        var MSpan = m2 - m1;
        //为负值 返回0；
        //一个月 内
        if (MSpan == 1) {
            //跨月 不足一个月
            if (Tmonth > month && Tday < day) {
                var endDay = new Date(year, month + 1, 0).getDate();
                console.log(endDay);
                return endDay - day + 1 + Tday;
            }
        } else {
            if (Tday >= day) {
                return MSpan + 1 + 31;
            }
        }
        // 同一个月
        if (MSpan == 0) {
            var daySpan = Tday - day + 1;
            //某个月的 第一天和最后一天
            if ((daySpan == 31 || daySpan == 28 || daySpan == 30 || daySpan == 27) && day == 1) {
                var endDay = new Date(year, month + 1, 0).getDate();
                if (Tday == endDay) {
                    return 1 + 31;
                }
            }
            return daySpan;
        }
        MSpan = MSpan > 12 ? 12 : MSpan;
        return MSpan + 31;
    }
}